<template>
	<view class="container">
		<!-- 头部导航 -->
		<view class="header-bag">
			<image src="../../static/img/icon-Get-into@3.png" class="back" @click="goBack"></image>
			<view class="header-center">
				<text class="header-title">选择团队</text>
			</view>
			<text class="save-btn" @click="saveSelection">保存</text>
		</view>
		
		<!-- 团队列表 -->
		<view class="box">
			<view class="team-list">
				<view 
					class="team-item" 
					:class="{ active: item.id === selectedTeamId }"
					v-for="(item, index) in teamList" 
					:key="item.id"
					@click="selectTeam(item)"
				>
					<text class="team-name">{{ item.group_name }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { investmentApi } from '../../api/index.js'
	
	export default {
		data() {
			return {
				teamList: [],
				selectedTeamId: '',
				selectedTeamName: ''
			}
		},
		
		onLoad() {
			this.loadTeamList();
			// 获取当前选中的团队
			this.selectedTeamId = uni.getStorageSync('sgroup_id') || '';
			this.selectedTeamName = uni.getStorageSync('sgroup_name') || '';
		},
		
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack();
			},
			
			// 加载团队列表
			async loadTeamList() {
				try {
					const result = await investmentApi.getGroupList();
					if (result.r === 0) {
						this.teamList = result.data.list || [];
					} else {
						uni.showToast({
							title: '获取团队列表失败',
							icon: 'none'
						});
					}
				} catch (error) {
					uni.showToast({
						title: '网络错误，请重试',
						icon: 'none'
					});
				}
			},
			
			// 选择团队
			selectTeam(team) {
				this.selectedTeamId = team.id;
				this.selectedTeamName = team.group_name;
			},
			
			// 保存选择
			saveSelection() {
				if (!this.selectedTeamName) {
					uni.showToast({
						title: '请选择团队！',
						icon: 'none'
					});
					return;
				}
				
				// 保存到本地存储
				uni.setStorageSync('sgroup_id', this.selectedTeamId);
				uni.setStorageSync('sgroup_name', this.selectedTeamName);
				
				// 返回上一页
				uni.navigateBack();
			}
		}
	}
</script>

<style scoped>
	.container {
		background-color: #EEEEEE;
		min-height: 100vh;
	}
	
	.header-bag {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		height: 88rpx;
		background-color: #FF5033;
		display: flex;
		align-items: center;
		z-index: 999;
	}
	
	.back {
		width: 40rpx;
		height: 64rpx;
		margin-left: 30rpx;
	}
	
	.header-center {
		flex: 1;
		text-align: center;
	}
	
	.header-title {
		font-size: 36rpx;
		color: white;
	}
	
	.save-btn {
		color: white;
		font-size: 30rpx;
		margin-right: 30rpx;
		padding: 10rpx 20rpx;
	}
	
	.box {
		margin-top: 88rpx;
		padding: 0 30rpx;
	}
	
	.team-list {
		background-color: #fff;
		border-radius: 10rpx;
		overflow: hidden;
	}
	
	.team-item {
		position: relative;
		height: 90rpx;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		border-bottom: 1px solid #EEEEEE;
		background-color: #fff;
	}
	
	.team-item:last-child {
		border-bottom: none;
	}
	
	.team-item.active {
		background-image: url('../../static/img/Right.png');
		background-repeat: no-repeat;
		background-position: calc(100% - 30rpx) center;
		background-size: 36rpx 26rpx;
	}
	
	.team-name {
		color: #4B4B4B;
		font-size: 30rpx;
	}
</style>